package com.example.onlineeducation.utils

import com.example.onlineeducation.data.json.BannerJson
import java.math.BigDecimal
import java.util.regex.Matcher
import java.util.regex.Pattern

/**
 * SimpleDes:
 * Creator: Sindi
 * Date: 2022-05-26
 * UseDes:
 */
object DataUtils {


    // 判断小数点后0-2位的数字的正则表达式
    private val NUMBER_PATTERN_TWO = Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$")

    /**
     * 校验是否为数字，且最多保留两位小数
     */
    fun checkStrIsNumTwo(str: String): Boolean {
        try {
            /** 先将str转成BigDecimal，然后在转成String  */
            BigDecimal(str).toString()
        } catch (e: Exception) {
            return false
        }
        val isNum: Matcher = NUMBER_PATTERN_TWO.matcher(str)
        return isNum.matches()
    }


    /*
    * 列表转String
    * */
    fun listToStr(list: List<String>): String {
        var str = ""
        for (i in list.indices) {
            str += if (i == list.size - 1) {
                list[i]
            } else {
                "${list[i]},"
            }
        }
        return str
    }


}